<template>
	<view>
		<bg-video></bg-video>
		<cu-custom bgColor="bg-main" :isBack="true">
			<block class="text-white" slot="content">设置</block>
		</cu-custom>
		<view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<button class=" bg-white  headimg" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
						<view class="cu-avatar lg round" :style="'background-image:url('+client.headimg+');'"></view>
					</button>
					<input class="text-right" type="nickname" v-model="client.nc" placeholder="请输入昵称" />
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<view class="title">电话</view>
					<input class="text-right" v-model="client.tel" placeholder="请输入电话" />
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<view class="title">人脸</view>
					<view @click="openTx" class="text-right flex-sub">
						{{client.tx ? "已设置" : "未设置"}}
					</view>
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view v-if="client.tx" class="bg-white">
				<image class="w-100" :src="client.tx" mode="aspectFill"></image>
			</view>
		</view>
		<view class="padding ">
			<button @click="save" class="cu-btn block bg-main lg">保存</button>
		</view>
	</view>
</template>

<script>
	import upload from "@/wx-cos/upload.js";
	export default {
		data: function() {
			return {
				txurl: "",
				client: this.store.state.client,
			};
		},
		onShareAppMessage: function(){
			return {
				title: this.store.state.title,
				path: '/pages/first?id='+this.store.state.client.clientid
			}
		},
		onLoad: function() {
			this.api("/client/info/get").send({
				clientid: this.client.clientid,
			}).then(res => {
				this.client = res.client;
			});
		},
		methods: {
			onChooseAvatar: function(e) {
				// 上传图片
				// 得到图片云服务地址
				upload.checkUploadFile(
					e.detail.avatarUrl,
					this.client.clientid,
					"headimg", pro => {}
				).then(data => {
					this.client.headimg = data.url;
					this.$forceUpdate();
				});
			},
			openTx: function() {
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					sizeType: ['original'],
					camera: "back",
					success: res => {
						upload.checkUploadFile(
							res.tempFiles[0].tempFilePath,
							this.client.clientid,
							"face", progressData => {
								console.log(progressData);
							}).then((data) => {
							this.client.tx = data.url;
						});
					}
				})
			},
			save: function() {
				this.api("/client/info/put").send({
					client: this.client
				}).then(res => {
					this.message.info("修改成功");
					uni.navigateBack();
				});
			}
		}
	}
</script>

<style scoped>
	.headimg {
		padding-left: 0;
		padding-right: 0;
	}

	.headimg::after {
		border: none;
	}
</style>